<?php
 
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LeaveController extends Controller
{
    //===============显示列表==============
    public function getIndex(Request $request){
        // dd($request['name']);
     if($request['name']){
            //搜索分页
            $res = DB::table('leave')
            ->join('person','person.id','=','leave.PersonID')
            ->select('leave.*','person.PersonName')
            ->where('person.PersonName','like','%'.$request['name'].'%')
            ->paginate(1);
            
        }else{
            //所有分页      
            $res = DB::table('leave')
            ->join('person','person.id','=','leave.PersonID')
            ->paginate(1);
        }
        // dd($info);
        $info = $request -> all();    
        return view('Personnel/Leave/index',['res'=>$res,'info'=>$info]);
}

    //=============添加========
    public function getAdd(){
        $username =session('username');
        //dd($username);
        $res = DB::table('department')
        ->join('person','person.DepartmentID','=','department.id')
        ->where('person.PersonName','=',$username)
        ->select('DepartmentName','person.id')
        ->get();
        //dd($res);
    	//解析模板
    	return view('Personnel/Leave/add',['res'=>$res]);
    }
    //======执行添加====
    public function postInsert(Request $request){

    	//接收数据
    	$date = $request ->all();
        //dd($date);
        //随机token
        $date['_token']=str_random(50);
    	//dd($date); 
        //添加sql语句
        $res = DB::table('leave')->insert($date);
        if($res){
            //成功跳转至列表页
            return redirect('/Personnel/Leave/index')->with('success','请假条添加成功');
        }else{
           return back()->with('error','假条添加失败');
        }                                                                                     
    }
    //================编辑=======
    public function getEdit($id){
        //$info= DB::table('Leave')->where('LeaveID',$id)->get();
        $info = DB::table('leave')
            ->join('person','person.id','=','leave.PersonID')
            ->where('LeaveID',$id)->get();
            $name = session('id');
               if($name!=$info[0]['id']){
                return back()->with('error','不是本人');
               }
        //dd($info);
        //解析模板显示编辑的页面
            return view('Personnel/Leave.edit',['info'=>$info,'LeaveID'=>$id]);
    }
    //================执行编辑
    public function postUpdate(Request $request){
        $info = $request ->all();
        $id = $info['LeaveID'];
        DB::table('Leave')->where('LeaveID',$id)->update($info);
        return redirect('Personnel/Leave/index')->with('修改成功');
    }   
   
    //=============================删除===========================
    public function getDel($id){
        //执行删除
        //dd($id);
        DB::table('Leave')->where('LeaveID',$id)->delete();
        //解析模板显示列表的页面
        return redirect('Personnel/Leave/index')->with('删除成功');
    }



}
?>